#ifndef __LAMP_H__
#define __LAMP_H__

#include "public.h"
#include <iostream>
#include <windows.h>
#include <list>



// 常用缩写 
// Cnt		----	Count
// ptr		----	pointer
// 

namespace UTSS 
{

	class CPhaseMgr;
	extern CPhaseMgr * ptr_pr;

	enum
	{
		NONE = 0,                       
		MAX_ROUTE_LAMP	= 8,
		MAX_PHASE_COUNT = 4,
		MAX_CROSS_NUM	= 16
	};


	// 灯
	// m_nColor 当前灯的颜色
	// m_nID	灯的编号
	class CLamp
	{
	public:
		enum eLampColor { c_none = 0, red, yellow, green };

	public:
		CLamp( eLampColor nColor = c_none );
		virtual ~CLamp() { }

	public:
		void SetColor( eLampColor nColor ) { m_nColor = nColor;	}

		eLampColor GetColor() const { return m_nColor; }

		Tuint32	GetID() const { return m_nID; }

	protected:
		eLampColor	m_nColor;
		Tuint32		m_nID;

	private:
		static Tuint32 m_nCount;

	// Forbidden
	private:
		CLamp( const CLamp & l );
		CLamp & operator= ( const CLamp & l );
	};

	
	// 交通灯
	// m_nTimer -----  当前相位的持续时间
	// m_nInfo	-----  当前的相位通行信息
	// m_cLamp	-----  交通灯的状态
	// m_nID	-----  交通灯的编号
	// m_bEnable-----  交通灯是否开启使用
	class CLampInfo
	{
	friend std::ostream & operator<< ( std::ostream & o, CLampInfo & lo );
	public:
		enum { AddTime = 10, NormalTime = 30, MaxTime = 40 };

		enum ePhaseInfo
		{
			stop		= 0,      // 禁止通行
			stright		= 2,      // 直行
			turn		= 4,      // 小转弯 
			cross		= 5,      // 大转弯 
			specific	= 7       // 未知情况
		};

	public:
		CLampInfo();
		CLampInfo( Tuint32 nTime, ePhaseInfo nInfo );
		CLampInfo( ePhaseInfo nInfo );
		virtual ~CLampInfo();

	public:
		void SetTime( Tuint32 nTime )    { m_nTime = nTime; }
		void SetInfo( ePhaseInfo nInfo ) { m_nInfo = nInfo; SetLamp(); }

		Tuint32    GetTime() const { return m_nTime; }
		ePhaseInfo GetInfo() const { return m_nInfo; }
		Tuint32    GetID()   const { return m_nID;   }

	public:
		void SetLamp();
		void ChgLamp();

	public:
		// TODO : m_bEnable 需要加锁/临界区
		bool IsEnable() const   { return m_bEnable; }
		void MakeEnable() { m_bEnable = true; }
		void MakeUnable() { m_bEnable = false;}

		void ReSetLamp() { m_nInfo = stop; SetLamp(); }
		void InitLamp()  { m_nInfo = stop; SetLamp(); }

		void Run();
		void Stop();

		// TODO : 
		// 这里注意线程同步
		// run stop 分别开2个线程，实时同步实现
		static DWORD WINAPI cp_run() ; 
		static DWORD WINAPI cp_stop();

	private:
		static Tuint32		m_nCount;
	
	protected:
		Tuint32				m_nTime;
		ePhaseInfo			m_nInfo;
		CLamp				m_cLamp[3];
		Tuint32				m_nID;        // 交通灯编号
		bool				m_bEnable;

	// Forbidden
	private:
		CLampInfo( const CLampInfo & lo );
		CLampInfo & operator=( const CLampInfo &lo );
	};	


	// 相位交通灯管理器
	// m_lLamps			----	交通灯链表
	// m_nUesd			----	使用的交通灯的个数
	// m_nCount			----	交通灯的总个数 
	class CPhaseMgr
	{
	public:
		typedef std::list<CLampInfo*> CList;

	private:
		CPhaseMgr();
		virtual ~CPhaseMgr();

	public:
		CList & AddLampInfo( CLampInfo * lo );			// 增加相位

		Tuint32 CheckCount() const { return m_lLamps.size(); }

		Tuint32 CheckUsedCnt();

	public:
		static CPhaseMgr * init_phase_mgr( CPhaseMgr * pr );

	private:
		CList			m_lLamps;
		Tuint32			m_nUsed;						// 使用相位数
		Tuint32			m_nCount;

	// Forbidden
	private:
		CPhaseMgr( const CPhaseMgr & pr );
		CPhaseMgr & operator= ( const CPhaseMgr & pr );

	};

	// 车道
	// m_eDirection		车道行驶方向
	// m_nLimitSpeed	车道限速
	// m_cLamp			车道上的交通灯
	typedef struct stRoad
	{
		enum { LIMIT_SPEED = 80 };
		stRoad( CLampInfo * lo, CLampInfo::ePhaseInfo = CLampInfo::specific )
			: m_eDirection( CLampInfo::specific )
#ifdef SPEED_LIMIT
			, m_nLimitSpeed( LIMIT_SPEED )
#elif defined( USE_LAMP )
			, m_cLamp( lo )
#endif
		{

		}
		
		stRoad( CLampInfo::ePhaseInfo = CLampInfo::specific )
			: m_eDirection( CLampInfo::specific )
#ifdef SPEED_LIMIT
			, m_nLimitSpeed( LIMIT_SPEED )
#elif defined( USE_LAMP )
			, m_cLamp( 0 )
#endif
		{

		}

		//---------------------------------------------
		CLampInfo::ePhaseInfo	m_eDirection;
#ifdef SPEED_LIMIT
		Tuint32					m_nLimitSpeed;
#elif defined( USE_LAMP )
		CLampInfo			   *m_pLamp;
#endif
	}ST_ROAD, *HST_ROAD;
	



#ifndef USE_LAMP
	// 车道信息
	// m_STRoad 车道
	// m_cLamp  交通灯信息
	class CRoadInfo 
	{
	public:
		CRoadInfo( CLampInfo::ePhaseInfo nInfo = CLampInfo::specific )
			: m_STRoad( nInfo )
			, m_cLamp()
		{

		}
		virtual ~CRoadInfo() { }

	public:
		CLampInfo::ePhaseInfo GetRoadInfo() const
	   	{
		   	return m_cLamp.GetInfo();
		}

		Tuint32 GetRoadTime() const
		{
			return m_cLamp.GetTime();
		}
#ifdef SPEED_LIMIT
		Tuint32 GetLimitSpeed() const
		{
			return m_STRoad.m_nLimitSpeed;
		}
		
		void SetLimitSpeed( Tuint32 nSpeed )
		{
			m_STRoad.m_nLimitSpeed = nSpeed;
		}
#endif
		CLampInfo::ePhaseInfo GetDirection() const
		{
			return m_STRoad.m_eDirection;
		}

		void SetDirection( CLampInfo::ePhaseInfo nInfo )
		{
			m_cLamp.SetInfo( nInfo );
		}
		
		void SetRoadTime( Tuint32 nTime ) 
		{
			m_cLamp.SetTime( nTime );
		}

		void Init()
		{
			m_cLamp.MakeEnable();
		}

		void Start()
		{
			m_cLamp.Run();
		}

		void MakeEnable()
		{
			m_cLamp.MakeEnable();
		}

		CLampInfo & GetLamp()
		{
			return m_cLamp;
		}

		void ChgLamp()
		{
			m_cLamp.ChgLamp();
		}

		ST_ROAD & GetRoad()
		{
			return m_STRoad;
		}

		bool IsEnable()
		{
			return m_cLamp.IsEnable();
		}

		void MakeUnEnable()
		{
			m_cLamp.MakeUnable();
		}
	

	protected:
		ST_ROAD		m_STRoad;
		CLampInfo	m_cLamp;
	};
#endif
		

	class CCross
	{
#ifdef USE_LAMP 
#define ROADINFO ST_ROAD
#else
#define ROADINFO CRoadInfo
#endif
	public:
		CCross( Tuint32 nRoadCnt = 4 );

		CCross( ROADINFO * ro, Tuint32 nRoadCnt = 4 );
		
		virtual ~CCross();

	public:
		void Run();


	protected:
		void Init();

	protected:
		Tuint32		m_nRoadCnt;
		ROADINFO   *m_pRoad;
	};

		
}

#endif
